#include <vector>

#include "Object3d.h"

Object3d::Object3d()
{
	coord_x = 0;
	coord_y = 0;
	coord_z = 0;
	size_x = 0;
	size_y = 0;
	size_z = 0;
}

Object3d::Object3d(float cx,float cy,float cz,float sx,float sy,float sz)
{
	coord_x = cx;
	coord_y = cy;
	coord_z = cz;
	size_x = sx;
	size_y = sy;
	size_z = sz;
}

void Object3d::Draw()
{
	glPushMatrix();
	glTranslatef(coord_x,coord_y,coord_z);
	glScalef(size_x,size_y,size_z);
	if (object.id_texture!=-1)
	{
		glBindTexture(GL_TEXTURE_2D, object.id_texture); // We set the active texture
		glEnable(GL_TEXTURE_2D); // Texture mapping ON
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
		glTexParameteri(GL_TEXTURE_2D,
                    GL_TEXTURE_MIN_FILTER,
                    GL_LINEAR_MIPMAP_LINEAR);
	}
	else
		glDisable(GL_TEXTURE_2D); // Texture mapping OFF
	int j;
	glBegin(GL_TRIANGLES); // glBegin and glEnd delimit the vertices that define a primitive (in our case triangles)
	for (j=0;j<object.polygons_qty;j++)
	{
		//----------------- FIRST VERTEX -----------------
		//Normal coordinates of the first vertex
		glNormal3f( object.normal[ object.polygon[j].a ].x,
			object.normal[ object.polygon[j].a ].y,
			object.normal[ object.polygon[j].a ].z);
		// Texture coordinates of the first vertex
		glTexCoord2f( object.mapcoord[ object.polygon[j].a ].u,
			object.mapcoord[ object.polygon[j].a ].v);
		// Coordinates of the first vertex
		glVertex3f( object.vertex[ object.polygon[j].a ].x,
			object.vertex[ object.polygon[j].a ].y,
			object.vertex[ object.polygon[j].a ].z);

		//----------------- SECOND VERTEX -----------------
		//Normal coordinates of the second vertex
		glNormal3f( object.normal[ object.polygon[j].b ].x,
			object.normal[ object.polygon[j].b ].y,
			object.normal[ object.polygon[j].b ].z);
		// Texture coordinates of the second vertex
		glTexCoord2f( object.mapcoord[ object.polygon[j].b ].u,
			object.mapcoord[ object.polygon[j].b ].v);
		// Coordinates of the second vertex
		glVertex3f( object.vertex[ object.polygon[j].b ].x,
			object.vertex[ object.polygon[j].b ].y,
			object.vertex[ object.polygon[j].b ].z);

		//----------------- THIRD VERTEX -----------------
		//Normal coordinates of the third vertex
		glNormal3f( object.normal[ object.polygon[j].c ].x,
			object.normal[ object.polygon[j].c ].y,
			object.normal[ object.polygon[j].c ].z);
		// Texture coordinates of the third vertex
		glTexCoord2f( object.mapcoord[ object.polygon[j].c ].u,
			object.mapcoord[ object.polygon[j].c ].v);
		// Coordinates of the Third vertex
		glVertex3f( object.vertex[ object.polygon[j].c ].x,
			object.vertex[ object.polygon[j].c ].y,
			object.vertex[ object.polygon[j].c ].z);
	}
	glEnd();
	glPopMatrix();

};